package com.lldpet.spring.cloud.demo.store.service.controller;

import com.lldpet.spring.cloud.demo.store.client.event.StoreUpdateEvent;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @desc：PaymentController
 * @author: guanliang.xue
 * @date: 2021/5/10 10:58
 * @version:
 * @since:
 */
@RestController
@RequestMapping("/store")
public class StoreController {

    /**
     * spring cloud bus 的配置
     */
    @Resource
    private BusProperties busProperties;

    /**
     * spring 的事件发布者
     */
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @GetMapping("update")
    public String update(@RequestParam(value ="destination", required = false ) String destination){

        // instanceId , spring cloud bus 默认实现的Id
        String instanceId = busProperties.getId();
        StoreUpdateEvent updateEvent = new StoreUpdateEvent("库存服务",instanceId,destination,"storeNo","userId");
        applicationEventPublisher.publishEvent(updateEvent);
        return "success";
    }
}
